<template>
  <div class="function-menu">
    <div class="toggle-ball"  @click="toggleMenu">
      ⚙️
    </div>
    <!-- @mouseenter="expandMenu" @mouseleave="startCollapseTimer" 鼠标悬浮事件-->
    <div class="menu-buttons" :class="{ active: isExpanded }">
      <!-- 左边的两个按钮 -->
      <div class="left-buttons">
        <button v-for="(btn, index) in buttons.slice(0, 2)" :key="index" @mouseenter="resetTimer"
          @click="handleButtonClick(btn.action)">
          <img :src="btn.icon" :alt="btn.action" class="button-icon" />
        </button>
      </div>
      <!-- 右边的两个按钮 -->
      <div class="right-buttons">
        <button v-for="(btn, index) in buttons.slice(2)" :key="index + 2" @mouseenter="resetTimer"
          @click="handleButtonClick(btn.action)">
          <img :src="btn.icon" :alt="btn.action" class="button-icon" />
        </button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router';
import { ref } from 'vue';
import { useDataSreenStore } from '@/store/data-screen';
import map from "@/assets/icons/map.svg"
import index from "@/assets/icons/whiteIndex.svg"
import star from "@/assets/icons/star.svg"
import earth from '@/assets/icons/earth.svg'


const isExpanded = ref(false);
let collapseTimer: ReturnType<typeof setTimeout> | null = null;


// 自定义图标路径
const buttons = [
  { icon: map, action: 'map' },
  { icon: index, action: 'index' },
  { icon: star, action: 'bg' },
  { icon: earth, action: 'earth' }
];

const expandMenu = () => {
  isExpanded.value = true;
  resetTimer();
};

const toggleMenu = () => {
  isExpanded.value = !isExpanded.value;
  resetTimer();
};

const startCollapseTimer = () => {
  if (collapseTimer) clearTimeout(collapseTimer);
  collapseTimer = setTimeout(() => {
    isExpanded.value = false;
  }, 50000);
};

const resetTimer = () => {
  if (collapseTimer) clearTimeout(collapseTimer);
  startCollapseTimer();
};

const router = useRouter();
const backgroundList = [1, 2, 3, 4];
const currentIndex = ref(0); // 记录当前索引

const handleButtonClick = (action: string) => {
  if (action == "map") {
    router.push("/dataScreen");
  } else if (action == "index") {
    router.push("/index");
  } else if (action == "bg") {
    // 轮播背景索引
    currentIndex.value = (currentIndex.value + 1) % backgroundList.length;
    useDataSreenStore().isStarBG = backgroundList[currentIndex.value];
  } else if (action == "earth") {
    useDataSreenStore().isEarth = !useDataSreenStore().isEarth;
  }
};
</script>

<style scoped>

/* // 功能菜单容器 */
.function-menu {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  transition: all 0.9s ease;
}

/* 功能球样式 */
.toggle-ball {
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 50%;
  background: linear-gradient(145deg, #1e90ff, #4169e1);
  box-shadow: 0 0.4rem 1rem rgba(0, 0, 0, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: white;
  font-size: 1.7rem;
  transition: transform 0.9s ease;
  position: absolute; /* 将球定位在中间 */
  z-index: 1; /* 确保球在按钮之上 */

  &:hover {
    transform: scale(1.1);
  }
}

/* 菜单按钮容器 */
.menu-buttons {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 14rem; /* 增加宽度以适应按钮布局 */
  opacity: 0;
  transform: scale(0);
  transition: all 0.5s ease;

  &.active {
    opacity: 1;
    transform: scale(1);
  }
}

/* 左边按钮组 */
.left-buttons {
  display: flex;
  gap: 0.05rem; /* 增加左边按钮之间的间距 */
  transform: translateX(-25%); /* 向左移动 */
}

/* 右边按钮组 */
.right-buttons {
  display: flex;
  gap: 0.05rem; /* 增加右边按钮之间的间距 */
  transform: translateX(25%); /* 向右移动 */
}

/* 按钮样式 */
button {
  width: 3rem;
  height: 3rem;
  border: none;
  border-radius: 50%;
  background: transparent;
  cursor: pointer;
  transition: all 0.9s ease;

  &:hover {
    transform: translateY(-0.2rem);
    background: rgba(255, 255, 255, 0.1);
  }
}

.button-icon {
  width: 1.7rem;
  height: 1.7rem;
  filter: brightness(0) invert(1);
  transition: filter 0.9s ease;

  &:hover {
    filter: brightness(1) invert(0);
  }
}
</style>